<template>
  <div>
    <div class="row">
      <div v-for="(item, idx) in list" :key="idx" v-drag-scroll class="item">
        <div class="item-t1">{{ item.type }} [{{ item.slot }}]</div>

        <div class="item-t2-wrap">
          <div
            v-for="(property, idx) in itemProperties(item)"
            :key="idx"
            class="item-t2"
          >
            <div class="l">
              {{ property.name }}
              <span v-if="property.add > 1" class="add-icon">
                +{{ property.add - 1 }}
              </span>
            </div>

            <div class="r">
              {{ property.valueStr }}
            </div>
          </div>
        </div>

        <div class="item-t3">
          {{ item['总分'] }}
        </div>
      </div>
    </div>

    <div class="row">
      <div class="item item2">
        <div class="item-t1">面板</div>

        <div class="item-t2-wrap">
          <div class="item-t2">
            <div class="l">暴击率</div>

            <div class="r">{{ panel('暴击率') }}</div>
          </div>

          <div class="item-t2">
            <div class="l">暴击伤害</div>

            <div class="r">{{ panel('暴击伤害') }}</div>
          </div>
        </div>

        <div class="item-t3">{{ score }}</div>
      </div>

      <div class="item item2">
        <div class="item-t1">属性</div>

        <div class="item-t2-wrap">
          <div
            v-for="(property, idx) in summaryValue"
            :key="idx"
            class="item-t2"
          >
            <div class="l">
              {{ property.name }}
              <span class="add-icon">
                {{ property.add }}
              </span>
            </div>

            <div class="r">
              {{ property.valueStr }}
            </div>
          </div>
        </div>
      </div>

      <div v-show="isShowRemarks" class="item item2">
        <div class="item-t1">1命:暴击率 上限16个（38.4%），面板58%</div>

        <div class="item-t1">0命:暴击率 上限20个（48%），面板68%</div>

        <div class="item-t1">四件套:12</div>

        <div class="item-t1">专武:20</div>

        <div class="item-t1">1命:10</div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    props: {
      list: {
        type: Array,
        required: true,
        default: () => {
          return []
        },
      },

      config: {
        type: Object,
        required: true,
        default: () => {
          return {
            equipSuitList: ['云岿如我', '啄木鸟电音', '折枝剑歌'],

            // 属性系数
            coeff: {
              暴击率: 1.57,
              暴击伤害: 1.57,
              生命值: 0.85,
              攻击力: 0.49,
              小攻击: 0.19,
              小生命: 0.38,
            },

            propertiesList: [
              '暴击率',
              '暴击伤害',
              '生命值',
              '攻击力',
              '小攻击',
              '小生命',
            ],
          }
        },
      },

      isShowRemarks: {
        type: Boolean,
        default: true,
      },
    },

    data() {
      return {
        idxItem: null,
      }
    },

    computed: {
      summaryValue() {
        // 遍历list, 取config.propertiesList的属性值求和
        let list = this.config.propertiesList
        return this.summary(list)
      },

      score() {
        let val = 0
        for (let item of this.list) {
          val += item['总分']
        }

        val = Math.round(val * 100) / 100
        return Number(val)
      },
    },

    mounted() {},

    methods: {
      toNumber(val) {
        // 转为数字 四舍五入保留1位小数
        val = Math.round(val * 10) / 10
        return Number(val)
      },

      // 返回item需要显示的属性
      itemProperties(item) {
        let map = [
          '暴击率',
          '暴击伤害',
          '生命值',
          '攻击力',
          '小攻击',
          '小生命',
          '以太伤害',
        ]
        let valueBase = {
          暴击率: 2.4,
          暴击伤害: 4.8,
          生命值: 6,
          攻击力: 3,
          小攻击: 19,
          小生命: 112,
          以太伤害: 3,
        }
        let list = []
        for (let key of map) {
          if (!item[key] && item[key] !== 0) {
            continue
          }
          let value = this.toNumber(valueBase[key] * item[key])
          list.push({
            name: key,
            add: item[key],
            value: value,
            valueStr: ['小攻击', '小生命'].includes(key) ? value : `${value}%`,
          })
        }
        return list
      },

      editItem(item, row) {
        for (let i of row) {
          i.selected = false
        }

        item.selected = true
        this.$emit('edit', item)
      },

      summary(list) {
        // 遍历list, 取config.propertiesList的属性值求和
        let sum = []
        for (let item of list) {
          sum.push({
            name: item,
            add: 0,
          })
        }
        for (let item of this.list) {
          for (let sumItem of sum) {
            if (item[sumItem.name]) {
              sumItem.add += item[sumItem.name]
            }
          }
        }
        let valueBase = {
          暴击率: 2.4,
          暴击伤害: 4.8,
          生命值: 6,
          攻击力: 3,
          小攻击: 19,
          小生命: 112,
          以太伤害: 3,
        }

        for (let item of sum) {
          let value = this.toNumber(valueBase[item.name] * item.add)
          item.value = value
          item.valueStr = ['小攻击', '小生命'].includes(item.name)
            ? value
            : `${value}%`
        }
        return sum
      },

      panel(key) {
        let val = this.summaryValue.find((item) => {
          return item.name === key
        })

        let valueBase = {
          暴击率: 19.4,
          暴击伤害: 66,
          生命值: 0.85,
          攻击力: 0.49,
        }

        let ss = val.value + valueBase[key]

        return ss
      },
    },
  }
</script>

<style scoped>
  .row {
    display: flex;
    gap: 15px;
    flex-shrink: 0;
    width: 100%;
    overflow-x: auto;
    padding: 15px;

    .item {
      flex-shrink: 0;

      width: 180px;
      background-color: #1a1a1a;
      padding: 10px;
      border-radius: 5px;
      display: flex;
      flex-direction: column;
      gap: 10px;

      &.selected {
        border: 4px solid #16d6e4;
      }

      &.hidden {
        display: none;
      }

      .item-t1 {
        font-size: 20px;
        font-weight: bold;
        color: #fff;
      }

      .item-t2-wrap {
        display: flex;
        flex-direction: column;
        gap: 10px;
        height: 114px;
      }
      .item-t2 {
        color: #d79b39;
        font-size: 16px;

        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        height: max-content;

        .add-icon {
          background-color: #d79b39;
          color: #161817;
          border-radius: 2px;
          font-size: 14px;
          padding: 0 4px;
          margin-left: 4px;
        }
      }
      .item-t3 {
        font-size: 18px;
        font-weight: bold;

        color: #de1a79;
      }
    }

    .item2 {
      width: 390px;
      .item-t1 {
        font-size: 16px;
      }
      .item-t2-wrap {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px 50px;
        height: 114px;
      }
    }
  }
</style>
